﻿using System;
using System.Data;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
using System.Web;
using System.Web.Caching;


using LMD.Constant;
using LMD.Lib.Oracle;

namespace Cached
{
    /// <summary>
    /// Summary description for ContactDataTypeTable
    /// </summary>
    public class ContactDataTypeTable : CacheAbstraction
    {
        public String GetContactDataTypeName(int iContactDataTypeId)
        {
            return GetContactDataTypeName(false, iContactDataTypeId);
        }
        public String GetContactDataTypeName(bool bForceRefresh, int iContactDataTypeId)
        {
            DataView dv = new DataView(
                GetContactDataType(bForceRefresh),
                "ID = " + iContactDataTypeId.ToString(),
                String.Empty,
                DataViewRowState.CurrentRows);

            if (dv.Count > 0)
            {
                return dv[0]["CODE"].ToString();
            }
            else
            {
                return String.Empty;
            }
        }        

        public DataTable GetContactDataType()
        {
            return GetContactDataType(false);
        }
        public DataTable GetContactDataType(bool bForceRefresh)
        {
            String sxCacheKey = KCache.P_CONTACT_DATA_TYPE;

            if(bForceRefresh)
            {
                HttpRuntime.Cache.Remove(sxCacheKey);
            }

            if (HttpRuntime.Cache[sxCacheKey] == null)
            {
                DBLib dbl = new DBLib();

                String sxQuery =
                    @"
                    select
                        P_CONTACT_DATA_TYPE_ID ID,
                        CODE,
                        P_INPUT_CONTROL_TYPE_ID CONTROL_TYPE_ID
                    FROM 
                        P_CONTACT_DATA_TYPE
					where NVL(VALID_TO, SysDate+1) > SysDate
                    order by
                        CODE ASC
                    ";

                DataTable dt = dbl.GetDataTable(sxQuery);

                AddToCache(sxCacheKey, dt, CacheType.OneHour);
                
            }
            return (DataTable)HttpRuntime.Cache[sxCacheKey];
        }        

        public override String GetCacheFilename()
        {
            return KCache.Filename.PARAMETER_TABLE;
        }
    }
}